<script lang="ts" setup>
// import { computed } from "vue"
import SidebarItemLink from "./SidebarItemLink.vue"

import { MenuData } from "@/layouts/components/Sidebar/types/menus"
// import path from "path-browserify"

interface Props {
  item: MenuData
  isCollapse?: boolean
  isTop?: boolean
  isFirstLevel?: boolean
  basePath?: string
  index?: number
}

const props = withDefaults(defineProps<Props>(), {
  isCollapse: false,
  isTop: false,
  isFirstLevel: true,
  basePath: ""
})
const isFirstMenu = () => {
  const menu = props.item
  return (
    menu.url != "#" &&
    menu.isOperation === "N" &&
    menu.isPermission == "Y" &&
    menu.state == "ENABLE" &&
    menu.isMainMenu == "Y"
  )
}
// 检查是否子菜单
const isSubMenu = (menu: MenuData) => {
  return menu.isOperation === "N" && menu.isPermission == "Y" && menu.state == "ENABLE" && menu.isMainMenu == "Y"
}

const currentChilds = props.item.childList?.filter((item: MenuData) => isSubMenu(item)) || []
</script>

<template>
  <div :class="{ 'simple-mode': props.isCollapse && !isTop, 'first-level': props.isFirstLevel }">
    <template v-if="isFirstMenu()">
      <SidebarItemLink :to="props.item.url">
        <el-menu-item :index="props.item.url">
          <ccIcon v-if="props.item.icon" name="iconliebiao" />
          <template v-if="props.item.name" #title>
            {{ props.item.name }}
          </template>
        </el-menu-item>
      </SidebarItemLink>
    </template>
    <el-sub-menu v-else :index="props.item.url + props.index" teleported>
      <template #title>
        <ccIcon v-if="props.item.icon" name="iconliebiao" />
        <span v-if="props.item.name">{{ props.item.name }}</span>
      </template>
      <template v-if="currentChilds.length > 0">
        <sidebar-item
          v-for="(child, i) in currentChilds"
          :key="child.url"
          :item="child"
          :index="i"
          :is-collapse="props.isCollapse"
          :is-first-level="false"
          :base-path="props.item.url"
        />
      </template>
    </el-sub-menu>
  </div>
</template>

<style lang="scss" scoped>
.svg-icon {
  min-width: 1em;
  margin-right: 12px;
  font-size: 18px;
}

.el-icon {
  width: 1em;
  margin-right: 12px;
  font-size: 18px;
}

.simple-mode {
  &.first-level {
    :deep(.el-sub-menu) {
      .el-sub-menu__icon-arrow {
        display: none;
      }
      span {
        visibility: hidden;
      }
    }
  }
}
</style>
